package zasm.zasm.util;

public final class ValidationUtils {

	public static void validateArgBetween(final int arg, final int min, final int max, final String argName) {
		validateArgBetween(arg, min, max, argName, null, null);
	}

	public static void validateArgBetween(final int arg, final int min, final int max, final String argName,
			final String minName, final String maxName) {
		if ((arg < min) || (arg > max)) {
			throw new IllegalArgumentException("Illegal " + argName + ": " + arg + ". Must be between "
					+ (minName != null ? minName + "=" : "") + min + " and " + (maxName != null ? maxName + "=" : "")
					+ max + ".");
		}
	}

	public static void validateArgMin(final int arg, final int min, final String argName) {
		validateArgMin(arg, min, argName, null);
	}

	public static void validateArgMin(final int arg, final int min, final String argName, final String minName) {
		if (arg < min) {
			illegalArg(arg, min, argName, "be greater than or equal to", minName);
		}
	}

	public static void validateArgMax(final int arg, final int max, final String argName) {
		validateArgMax(arg, max, argName, null);
	}

	public static void validateArgMax(final int arg, final int max, final String argName, final String maxName) {
		if (arg > max) {
			illegalArg(arg, max, argName, "be less than or equal to", maxName);
		}
	}

	public static void validateArgDivides(final int arg, final int dividend, final String argName) {
		validateArgDivides(arg, dividend, argName, null);
	}

	public static void validateArgDivides(final int arg, final int dividend, final String argName,
			final String dividendName) {
		if ((dividend % arg) != 0) {
			illegalArg(arg, dividend, argName, "evenly divide", dividendName);
		}
	}

	public static void illegalArg(final int arg, final int bound, final String argName, final String condition,
			final String boundName) {
		throw new IllegalArgumentException("Illegal " + argName + ": " + arg + ". Must " + condition + " "
				+ (boundName != null ? boundName + "=" : "") + bound + ".");
	}

	private ValidationUtils() {}

}
